<?php

require_once(dirname(__FILE__).'/ApiPoolProtocol.php');

class ApiPoolRequest
{
	public function getRequestSrc() {
		return $this->request_src_;
	}
	public function setRequestSrc($request_src) {
		$this->request_src_ = $request_src;
	}
	
	public function getServiceName() {
		return $this->service_name_;
	}
	public function setServiceName($service_name) {
		$this->service_name_ = $service_name;
	}
	
	public function getApiName() {
		return $this->api_name_;
	}
	public function setApiName($api_name) {
		$this->api_name_ = $api_name;
	}
	
	public function getParamList() {
		return $this->param_list_;
	}
	public function setParamList($param_list) {
		$this->param_list_ = $param_list;
	}
	
	public function getId() {
		return $this->id_;
	}
	public function setId($id) {
		$this->id_ = $id;
	}
	
	public function getVersion() {
		return $this->version_;
	}
	public function Version($version) {
		$this->version_ = $version;
	}
	
	public function getCryptType() {
		return $this->crypt_type_;
	}
	public function setCryptType($type) {
		$this->crypt_type_ = $type;
	}
	
	public function getCryptKey() {
		return $this->crypt_key_;
	}
	public function setCryptKey($key) {
		$this->crypt_key_ = $key;
	}
	
	public function makePackage() {
		$json_array = array(
			ApiPoolProtocol::getServiceNameApiNameKey() => ($this->service_name_.'.'.$this->api_name_),
			ApiPoolProtocol::getRequestSrcKey() => $this->request_src_,
			ApiPoolProtocol::getParamListKey() => $this->param_list_,
			ApiPoolProtocol::getIdKey() => $this->id_,
			ApiPoolProtocol::getVersionKey() => '1.1',
		);
		
		if (!is_null($this->crypt_type_)) {
			$json_array[ApiPoolProtocol::getCryptTypeKey()] = $this->crypt_type_;
			$json_array[ApiPoolProtocol::getCryptKeyKey()] = $this->crypt_key_;
		}
		
		$json_encode = json_encode($json_array);
		if ($json_encode == null) {
			return null;
		}
		
		return ApiPoolProtocol::makePackage($json_encode);
	}
	
	public static function create($request, $request_src, $service_name, $api_name, $param_list) {
		if (! ApiPoolProtocol::getRequestSrcValidator()->validate($request_src)) {
			return false;
		}
		if (! ApiPoolProtocol::getServiceNameValidator()->validate($service_name)) {
			return false;
		}
		if (! ApiPoolProtocol::getApiNameValidator()->validate($api_name)) {
			return false;
		}
		
		$request->setRequestSrc($request_src);
		$request->setServiceName($service_name);
		$request->setApiName($api_name);
		$request->setParamList($param_list);
		
		return true;
	}
	
	private $request_src_;
	private $service_name_;
	private $api_name_;
	private $param_list_;
	private $id_;
	private $version_;
	private $crypt_type_;
	private $crypt_key_;
}

?>